﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace TB_WS.Class
{
    public class DingTalk
    {
        public static DataSet get_DingTalk_Open_Info(string Seller_Nick)
        {
            string sql = "select * from DingTalk_Info where Seller_Nick='" + Seller_Nick + "'";
            return Class.Unit.db_Conn.select(sql);
        }

        public static int Update_DingTalk_Info(string Seller_Nick,string App_Key,string App_Secret,string App_ID)
        {
            string sql = "update DingTalk_Info set App_Key='" + App_Key + "',App_Secret='" + App_Secret + "',App_ID='" + App_ID + "' where Seller_Nick='" + Seller_Nick + "'";
            return Class.Unit.db_Conn.Nonquery(sql);
        }

        public static string GetAccessToken(string Seller_Nick)
        {
            string sql = "select * from DingTalk_Info where Seller_Nick='" + Seller_Nick + "'";
            DataSet Info_DST = Class.Unit.db_Conn.select(sql);
            string App_Key = Info_DST.Tables[0].Rows[0][2].ToString();
            string App_Secret= Info_DST.Tables[0].Rows[0][3].ToString();
            return LeShiTao_Library.DingTalk.HTTP.GET_ACCESSTOKEN(App_Key, App_Secret);
        }

        public static string Send_QY_Message_ToParty(string Seller_Nick,string ToParty,string TextStr)
        {
            string sql = "select * from DingTalk_Info where Seller_Nick='" + Seller_Nick + "'";
            DataSet Info_DST = Class.Unit.db_Conn.select(sql);
            string App_Key = Info_DST.Tables[0].Rows[0][2].ToString();
            string App_Secret = Info_DST.Tables[0].Rows[0][3].ToString();
            return LeShiTao_Library.DingTalk.Query_Message.Send_QY_TEXT_Message_ToParty(ToParty, GetAccessToken(Seller_Nick),
                App_Key, App_Secret);
        }

        public static string Get_Deparment_List(string Seller_Nick)
        {
            string sql = "select * from DingTalk_Info where Seller_Nick='" + Seller_Nick + "'";
            DataSet Info_DST = Class.Unit.db_Conn.select(sql);
            string App_Key = Info_DST.Tables[0].Rows[0][2].ToString();
            string App_Secret = Info_DST.Tables[0].Rows[0][3].ToString();
            return LeShiTao_Library.DingTalk.Query_Message.Get_Deparment_List(GetAccessToken(Seller_Nick));
        }
        

        public static DataSet Get_Robot_Type()
        {
            string sql = "select * from Robot_Type";
            return Class.Unit.db_Conn.select(sql);
        }

        public static DataSet Get_Robot_URL(int Seller_ID)
        {
            string sql = "select Type_Name,DingTalk_Robot.Robot_URL from DingTalk_Robot,Robot_Type where Robot_Type.id=DingTalk_Robot.Type_ID and Seller_ID=" + Seller_ID.ToString();
            return Class.Unit.db_Conn.select(sql);
        }

        public static int Insert_Robot_URL(string Robot_URL,int Seller_ID,int Robot_Type)
        {
            string sql = "insert into DingTalk_Robot values(" + Robot_Type + ",'" + Robot_URL + "'," + Seller_ID + ")";
            return Class.Unit.db_Conn.Nonquery(sql);
        }
    }
}
